#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class DeviceRcdProvider : CY.GFive.Core.Providers.DALProvider.IDeviceRcdProvider
	{
		#region Sql
		private static readonly string SqlSelectDeviceRcd = "SELECT [Id], [Code],[RcdDate],[DevUserCode],[DevUser],[RcdDetail],[UseNum],[AssInCharge],[AssInChargeCode],[AssView],[AssResult],[EquipCate],[RcdType] FROM [DeviceRcd] WHERE  [Id]=@Id";

		private static readonly string SqlInsertDeviceRcd = "INSERT INTO [DeviceRcd]([Code],[RcdDate],[DevUserCode],[DevUser],[RcdDetail],[UseNum],[AssInCharge],[AssInChargeCode],[AssView],[AssResult],[EquipCate],[RcdType]) values(@Code,@RcdDate,@DevUserCode,@DevUser,@RcdDetail,@UseNum,@AssInCharge,@AssInChargeCode,@AssView,@AssResult,@EquipCate,@RcdType);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateDeviceRcd = "UPDATE [DeviceRcd] SET [Code] = @Code,[RcdDate] = @RcdDate,[DevUserCode] = @DevUserCode,[DevUser] = @DevUser,[RcdDetail] = @RcdDetail,[UseNum] = @UseNum,[AssInCharge] = @AssInCharge,[AssInChargeCode] = @AssInChargeCode,[AssView] = @AssView,[AssResult] = @AssResult,[EquipCate] = @EquipCate,[RcdType] = @RcdType WHERE  [Id]=@Id";

		private static readonly string SqlDeleteDeviceRcd = "DELETE [DeviceRcd] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllDeviceRcd = "SELECT [Id], [Code],[RcdDate],[DevUserCode],[DevUser],[RcdDetail],[UseNum],[AssInCharge],[AssInChargeCode],[AssView],[AssResult],[EquipCate],[RcdType] FROM [DeviceRcd]";
		#endregion

		#region IDeviceRcdProvider Members
		public Core.Business.DeviceRcd Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectDeviceRcd);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.DeviceRcd deviceRcd = new Core.Business.DeviceRcd();

				if (!reader.IsDBNull(0)) deviceRcd.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) deviceRcd.Code = reader.GetString(1);
				if (!reader.IsDBNull(2)) deviceRcd.RcdDate = reader.GetDateTime(2);
				if (!reader.IsDBNull(3)) deviceRcd.DevUserCode = reader.GetString(3);
				if (!reader.IsDBNull(4)) deviceRcd.DevUser = reader.GetString(4);
				if (!reader.IsDBNull(5)) deviceRcd.RcdDetail = reader.GetString(5);
				if (!reader.IsDBNull(6)) deviceRcd.UseNum = reader.GetInt32(6);
				if (!reader.IsDBNull(7)) deviceRcd.AssInCharge = reader.GetString(7);
				if (!reader.IsDBNull(8)) deviceRcd.AssInChargeCode = reader.GetString(8);
				if (!reader.IsDBNull(9)) deviceRcd.AssView = reader.GetString(9);
				if (!reader.IsDBNull(10)) deviceRcd.AssResult = reader.GetString(10);
				if (!reader.IsDBNull(11)) deviceRcd.EquipCate = reader.GetString(11);
				if (!reader.IsDBNull(12)) deviceRcd.RcdType = reader.GetString(12);

				return deviceRcd;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.DeviceRcd deviceRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Code", SqlDbType.NVarChar, deviceRcd.Code);
			sql.AddParameter("@RcdDate", SqlDbType.DateTime, deviceRcd.RcdDate);
			sql.AddParameter("@DevUserCode", SqlDbType.NVarChar, deviceRcd.DevUserCode);
			sql.AddParameter("@DevUser", SqlDbType.NVarChar, deviceRcd.DevUser);
			sql.AddParameter("@RcdDetail", SqlDbType.NVarChar, deviceRcd.RcdDetail);
			sql.AddParameter("@UseNum", SqlDbType.Int, deviceRcd.UseNum);
			sql.AddParameter("@AssInCharge", SqlDbType.NVarChar, deviceRcd.AssInCharge);
			sql.AddParameter("@AssInChargeCode", SqlDbType.NVarChar, deviceRcd.AssInChargeCode);
			sql.AddParameter("@AssView", SqlDbType.NVarChar, deviceRcd.AssView);
			sql.AddParameter("@AssResult", SqlDbType.NVarChar, deviceRcd.AssResult);
			sql.AddParameter("@EquipCate", SqlDbType.NVarChar, deviceRcd.EquipCate);
			sql.AddParameter("@RcdType", SqlDbType.NVarChar, deviceRcd.RcdType);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertDeviceRcd);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) deviceRcd.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.DeviceRcd deviceRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Code", SqlDbType.NVarChar, deviceRcd.Code);
			sql.AddParameter("@RcdDate", SqlDbType.DateTime, deviceRcd.RcdDate);
			sql.AddParameter("@DevUserCode", SqlDbType.NVarChar, deviceRcd.DevUserCode);
			sql.AddParameter("@DevUser", SqlDbType.NVarChar, deviceRcd.DevUser);
			sql.AddParameter("@RcdDetail", SqlDbType.NVarChar, deviceRcd.RcdDetail);
			sql.AddParameter("@UseNum", SqlDbType.Int, deviceRcd.UseNum);
			sql.AddParameter("@AssInCharge", SqlDbType.NVarChar, deviceRcd.AssInCharge);
			sql.AddParameter("@AssInChargeCode", SqlDbType.NVarChar, deviceRcd.AssInChargeCode);
			sql.AddParameter("@AssView", SqlDbType.NVarChar, deviceRcd.AssView);
			sql.AddParameter("@AssResult", SqlDbType.NVarChar, deviceRcd.AssResult);
			sql.AddParameter("@EquipCate", SqlDbType.NVarChar, deviceRcd.EquipCate);
			sql.AddParameter("@RcdType", SqlDbType.NVarChar, deviceRcd.RcdType);
			sql.AddParameter("@Id", SqlDbType.Int, deviceRcd.Id);
			sql.ExecuteSql(SqlUpdateDeviceRcd);
		}

		public void Delete(Core.Business.DeviceRcd deviceRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, deviceRcd.Id);
			sql.ExecuteSql(SqlDeleteDeviceRcd);
		}

		public IList<Core.Business.DeviceRcd> GetAllDeviceRcd()
		{
			IList<Core.Business.DeviceRcd> deviceRcdlist = new List<Core.Business.DeviceRcd>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllDeviceRcd);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.DeviceRcd deviceRcd = new Core.Business.DeviceRcd();

					if (!reader.IsDBNull(0)) deviceRcd.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) deviceRcd.Code = reader.GetString(1);
					if (!reader.IsDBNull(2)) deviceRcd.RcdDate = reader.GetDateTime(2);
					if (!reader.IsDBNull(3)) deviceRcd.DevUserCode = reader.GetString(3);
					if (!reader.IsDBNull(4)) deviceRcd.DevUser = reader.GetString(4);
					if (!reader.IsDBNull(5)) deviceRcd.RcdDetail = reader.GetString(5);
					if (!reader.IsDBNull(6)) deviceRcd.UseNum = reader.GetInt32(6);
					if (!reader.IsDBNull(7)) deviceRcd.AssInCharge = reader.GetString(7);
					if (!reader.IsDBNull(8)) deviceRcd.AssInChargeCode = reader.GetString(8);
					if (!reader.IsDBNull(9)) deviceRcd.AssView = reader.GetString(9);
					if (!reader.IsDBNull(10)) deviceRcd.AssResult = reader.GetString(10);
					if (!reader.IsDBNull(11)) deviceRcd.EquipCate = reader.GetString(11);
					if (!reader.IsDBNull(12)) deviceRcd.RcdType = reader.GetString(12);

					deviceRcd.MarkOld();
					deviceRcdlist.Add(deviceRcd);
				}
				reader.Close();
			}
			return deviceRcdlist;
		}
		#endregion
	}
}
